//
// Created by hsj on 2024-1-15.
//


#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <fcntl.h>

#ifdef WIN32
//windows minGW64
//inet_pton函数：_WIN32_WINNT
#ifdef _WIN32_WINNT
#undef _WIN32_WINNT
#endif
#define _WIN32_WINNT 0x0600
#include <winsock2.h>
#include <ws2tcpip.h>
//#pragma comment(lib,"ws2_32.lib")
#else
//linux  gcc
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/select.h>
#endif
#define BUF_SIZE 100
void error_handling(char *buf);
int main(int argc,char *argv[]){
    int sock;
    char buf[BUF_SIZE] ;
    struct sockaddr_in serv_addr;
    FILE * readfp;
    FILE * writefp;
    char *ip = "127.0.0.1";
    char port[] = "3399";

    sock=socket(PF_INET, SOCK_STREAM, 0);
    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family=AF_INET;
    serv_addr.sin_addr.s_addr=inet_addr(/*argv[1]*/ip);
    serv_addr.sin_port=htons(atoi(/*argv[2]*/port));

    connect(sock, (struct sockaddr* )&serv_addr, sizeof(serv_addr));

    readfp=fdopen(sock, "r");
    writefp=fdopen(sock, "w");

    while (1){
        if(fgets(buf, sizeof(buf), readfp)==NULL)
            break;
        fputs(buf,stdout);
        fflush(stdout);
    }

    fputs("FROM CLIENT: Thank you! \n", writefp);
    fflush(writefp);
    fclose(writefp);
    fclose(readfp);

    return EXIT_SUCCESS;
}

void error_handling( char *message){
    fputs (message, stderr);
    fputc('\n', stderr);
    exit(1);
}